# @Time:2021/6/20 19:58
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import pygame
import os
from actor.swk import SWK
from scene import SceneResult
from scene.tiled_scene import TileScene
from scene.fade_scene import FadeScene, SceneStatus
from pygame import QUIT, K_y, KEYDOWN
import pytmx
from actor.cute import Cute


# 开始界面
class StartScene(object):
    scene_result = SceneResult.Ongoing

    def __init__(self, swk: SWK, surface: pygame.Surface):
        self.screen = surface
        img_path = os.path.join('../resource', 'tmx', 'start.tmx')
        self.tiled_scene = TileScene(img_path)
        self.back_surface = self.tiled_scene.surface
        self.fade_scene = FadeScene(self.back_surface)
        self.temp_surface = pygame.Surface((800, 600))
        self.swk_player = swk
        self.obstacle_group = pygame.sprite.Group()
        self.win_pos_x = 0
        self.win_pos_y = 0
        self.dx_dy = [0, 0]
        self.init_actor()

        pygame.mixer.init()
        pygame.mixer.music.load('../resource/sound/backgroud.mp3')
        pygame.mixer.music.play(-1, 50)

    def init_actor(self):
        for group in self.tiled_scene.tiled.tmx_data.objectgroups:
            if isinstance(group, pytmx.pytmx.TiledObjectGroup):
                if group.name == 'npc':
                    for obj in group:
                        self.cute = Cute(obj.x, obj.y)
                if group.name == 'actor':  # 判断是不是actor对象组
                    for obj in group:  # 遍历actor对象组
                        if obj.name == 'sun':  # 判断是否有孙悟空
                            self.swk_player.set_pos(obj.x, obj.y)  # 将孙悟空全局的位置赋值给pos_x和pos_y
                            self.win_pos_x = obj.x - 400  # 将孙悟空显示在中间   窗口的大小是(800,600)
                            self.win_pos_y = obj.y - 300
                if group.name=='obstacle':
                    for obj in group:
                        obs = pygame.sprite.Sprite()
                        obs.rect=pygame.Rect(obj.x,obj.y,obj.width,obj.height)
                        self.obstacle_group.add(obs)

    def get_cur_surface(self):
        self.win_surface = self.fade_scene.get_back_image(self.win_pos_x, self.win_pos_y)
        self.temp_surface.blit(self.win_surface, (0, 0))
        self.swk_player.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        self.cute.draw(self.temp_surface, self.win_pos_x, self.win_pos_y)
        self.screen.blit(self.temp_surface, (0, 0))

    def run(self):
        scene_exit = False
        clock = pygame.time.Clock()
        while not scene_exit:
            key_down = False
            self.dx_dy = [0, 0]
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.scene_result = SceneResult.Quit
                    scene_exit=True
                if event.type == KEYDOWN:
                    key_down = True
                    pressed_key = event.key
                    self.dx_dy = self.swk_player.key_move(pressed_key, key_down,self.obstacle_group)
                    if key_down == True:
                        self.win_pos_x += self.dx_dy[0]
                        self.win_pos_y += self.dx_dy[1]
                        if self.cute.is_stop and pressed_key == K_y:
                            # 转入村庄场景
                            self.fade_scene.set_status(SceneStatus.Out)
                            self.cute.is_stop = False
                if self.fade_scene.get_out():
                    self.scene_result = SceneResult.Next
                    scene_exit = True

                if self.win_pos_x < 0:
                    self.win_pos_x = 0
                if self.win_pos_y < 0:
                    self.win_pos_y = 0
                if self.win_pos_x > (3780 - 800):
                    self.win_pos_x = 3780 - 800
                if self.win_pos_y > (2395 - 600):
                    self.win_surface = 2395 - 600
                if self.fade_scene.status != SceneStatus.Out:
                    collide_result = self.cute.collide(self.swk_player)
                self.cute.Move()
                self.get_cur_surface()
                pygame.display.update()
                clock.tick(15)
